package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.dto.GoodsDTO;
import com.wcards.entity.Goods;
import com.wcards.mapper.GoodsMapper;
import com.wcards.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
商品表
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /*
     * 添加商品
     * */
    @Override
    public R<String> createGoods(GoodsDTO goodsDTO) {
        goodsMapper.insert(goodsDTO);
        return R.success("添加成功!");
    }
    /*
     * 删除商品
     * */

    @Override
    public R<String> deleteGoods(Integer id) {
        goodsMapper.deleteById(id);

        return R.success("删除成功!");
    }

    /*
     * 修改商品
     * */

    @Override
    public R<String> updateGoods(GoodsDTO goodsDTO) {
        goodsMapper.updateById(goodsDTO);
        return R.success("修改成功!");
    }

    /*
     * 查询商品
     * */

    @Override
    public R<Page<Goods>> findGoods(GoodsDTO goodsDTO, Integer pageSize, Integer pageNumber) {
        Page<Goods> goodsIPage = new Page<>(pageSize, pageNumber);
        //根据商品名称模糊查询
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.like(Goods::getGoodsName, goodsDTO.getGoodsName());
        goodsMapper.selectPage(goodsIPage, lqw);
        return R.success(goodsIPage);
    }
}
